﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Common;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils.Menu;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using ERP_RORZE_DLL.Product.Base;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_PERMISSION.Base;
using ERP_RORZE_SYSTEM;
using BaseDAO = ERP_RORZE_TOOL.DBAccess.BaseDAO;

namespace ERP_RORZE_PRODUCT.Product.WTS
{
    public partial class ctrlLaborWTSDetail : DevExpress.XtraEditors.XtraUserControl
    {
        DXPopupMenu menu;
        DataTable dt;
        ClsWTSDAO clsWTSDAO = new ClsWTSDAO();
        private LogUtils log;
        ClsLaborWTSTable b = new ClsLaborWTSTable();

        public ctrlLaborWTSDetail()
        {
            InitializeComponent();
            InitPopupMenu();
            gridView1.BestFitColumns();
            log = new LogUtils();
            this.dropbtExport.DropDownControl = menu;
        }
        void Reload(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            dt = clsWTSDAO.GetAllLaborWTS();
            gridControl1.DataSource = dt;
            gridView1.BestFitColumns();
            Cursor.Current = Cursors.Default;

        }
        private void btnAddNew_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            //frm_LaborWTSInput frmAdd = new frm_LaborWTSInput();
            frm_LaborWTSAdd frmAdd = new frm_LaborWTSAdd();
            frmAdd.Reload += new EventHandler(Reload);
            frmAdd.Show();
            Cursor.Current = Cursors.Default;

        }

        private void btEdit_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            //frm_LaborWTSEdit frmEdit = new frm_LaborWTSEdit();
            frm_LaborWTSUpdate frmEdit = new frm_LaborWTSUpdate();
            frmEdit.Reload += new EventHandler(Reload);
            frmEdit.ID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ID").ToString();
            frmEdit.ClampTime = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Clamptime").ToString();
            frmEdit.Date = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "date").ToString();
            frmEdit.MachineID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MachineID").ToString();
            frmEdit.MONo = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MONo").ToString();
            frmEdit.NGQty = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGQty").ToString();
            frmEdit.Note1 = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Note1").ToString();
            frmEdit.Note2 = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Note2").ToString();
            frmEdit.OKQty = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "OKQty").ToString();
            frmEdit.OptionID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "OptionID").ToString();
            frmEdit.ProTime = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ProTime").ToString();
            frmEdit.Shift = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Shift").ToString();
            frmEdit.StaffID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "StaffID").ToString();
            frmEdit.Time = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "time").ToString();
            frmEdit.WorkID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "WorkID").ToString();
            frmEdit.Show();
            Cursor.Current = Cursors.Default;
        }

        private void btnWTStoGA_Click(object sender, EventArgs e)
        {
            frm_LaborWTStoGA frmToGA = new frm_LaborWTStoGA();
            frmToGA.Show();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            btEdit_Click(sender, e);
        }

        private void ctrlLaborWTSDetail_Load(object sender, EventArgs e)
        {
            Reload(sender, e);
        }

        private void btnReload_Click(object sender, EventArgs e)
        {
            Reload(sender, e);
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            Reload(sender, e);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (gridView1.SelectedRowsCount > 0)
            {
                var row = new DataRow[gridView1.SelectedRowsCount];
                var laborId = new string[gridView1.SelectedRowsCount];

                for (int i = 0; i < gridView1.SelectedRowsCount; i++)
                {
                    row[i] = gridView1.GetDataRow(gridView1.GetSelectedRows()[i]);
                    laborId[i] = row[i]["ID"].ToString();
                }


                if (MessageBox.Show("Bạn có thực sự muốn xóa dữ liệu!", "Message", MessageBoxButtons.OKCancel,
                                MessageBoxIcon.Question) == DialogResult.OK)
                {
                    for (int i = 0; i < laborId.Length; i++)
                    {
                        try
                        {
                            b.ID = int.Parse(laborId[i]);
                            clsWTSDAO.DeleteLaborWTS(b);

                            //write Log
                            log.Table = LogConstant.Table.Product.LaborWTS;
                            log.Task = LogConstant.Task.Delete;
                            log.Description = "Date: " + row[i]["date"] + "; MONo: " + row[i]["MONo"] + "; MachineID: " + row[i]["MachineID"] + "; OptionID: " + row[i]["OptionID"] + "; Shift: " + row[i]["Shift"] + "; WorkID: " + row[i]["WorkID"] + "; Time: " + row[i]["time"] + "; ProTime: " + row[i]["ProTime"] + "; ClampTime: " + row[i]["Clamptime"];
                            log.WriteLog();

                        }
                        catch (Exception ex)
                        {

                            MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                        }
                    }
                    MessageBox.Show("Đã xóa!");
                    Reload(sender, e);

                }
            }
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            btnDelete_Click(sender, e);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            btnAddNew_Click(sender, e);
        }

        //Export to excel button

        protected virtual void InitPopupMenu()
        {
            menu = new DXPopupMenu();
            menu.Items.Add(new DXMenuItem("Export all", new EventHandler(OnItemExportAll), null, null, null, null));
            menu.Items.Add(new DXMenuItem("Export selected row(s)", new EventHandler(OnItemExportSelectedRows), null, null, null, null));

        }
        void OnItemExportAll(object sender, EventArgs e)
        {
            clsConstantsFuction.ExportToExcel(gridView1, 1, true);
        }
        void OnItemExportSelectedRows(object sender, EventArgs e)
        {
            clsConstantsFuction.ExportToExcel(gridView1, 0, true);
        }

        private void btImport_Click(object sender, EventArgs e)
        {
            #region

            //FileDialog fileDialog = new OpenFileDialog();
            //fileDialog.Filter = "Excel 2007-2013|*.xlsx|Excel 97-2003 (*.xls)|*.xls";

            //int rowCount = 0;

            //INISettings iniSettings = new INISettings();
            //string connectString = iniSettings.SQLConnectionString;
            //OleDbConnection connection = new OleDbConnection();
            //OleDbCommand command = null;

            //// Create DbDataReader to Data Worksheet
            //DbDataReader dr = null;

            //// Bulk Copy to SQL Server 
            //SqlBulkCopy bulkInsert = new SqlBulkCopy(connectString);

            //if (fileDialog.ShowDialog() == DialogResult.OK)
            //{

            //    try
            //    {
            //        string path = fileDialog.FileName;

            //        // Connection String to Excel Workbook
            //        string excelConnectionString =
            //            string.Format(
            //                "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0;Persist Security Info=False",
            //                path);

            //        connection.ConnectionString = excelConnectionString;
            //        connection.Open();

            //        command = new OleDbCommand("select  COUNT(*) from [Sheet1$]", connection);
            //        rowCount = (int) command.ExecuteScalar();

            //        command = new OleDbCommand("select * from [Sheet1$]", connection);
            //        dr = command.ExecuteReader();

            //        bulkInsert.DestinationTableName = "[rrc_database].[dbo].[242_LaborWTS]";

            //        SqlBulkCopyColumnMapping date = new SqlBulkCopyColumnMapping("date", "Date");
            //        bulkInsert.ColumnMappings.Add(date);

            //        SqlBulkCopyColumnMapping toolId = new SqlBulkCopyColumnMapping("Shift", "Shift");
            //        bulkInsert.ColumnMappings.Add(toolId);

            //        SqlBulkCopyColumnMapping machineId = new SqlBulkCopyColumnMapping("StaffID", "StaffID");
            //        bulkInsert.ColumnMappings.Add(machineId);

            //        SqlBulkCopyColumnMapping mistakeId = new SqlBulkCopyColumnMapping("MachineID", "MachineID");
            //        bulkInsert.ColumnMappings.Add(mistakeId);

            //        SqlBulkCopyColumnMapping statusName = new SqlBulkCopyColumnMapping("MONo", "MONo");
            //        bulkInsert.ColumnMappings.Add(statusName);

            //        SqlBulkCopyColumnMapping lossNo = new SqlBulkCopyColumnMapping("OptionID", "OptionID");
            //        bulkInsert.ColumnMappings.Add(lossNo);

            //        SqlBulkCopyColumnMapping partNo = new SqlBulkCopyColumnMapping("WorkID", "WorkID");
            //        bulkInsert.ColumnMappings.Add(partNo);

            //        SqlBulkCopyColumnMapping quantity = new SqlBulkCopyColumnMapping("time", "time");
            //        bulkInsert.ColumnMappings.Add(quantity);

            //        SqlBulkCopyColumnMapping processStatusId = new SqlBulkCopyColumnMapping("OKQty", "OKQty");
            //        bulkInsert.ColumnMappings.Add(processStatusId);

            //        SqlBulkCopyColumnMapping rotateId = new SqlBulkCopyColumnMapping("NGQty", "NGQty");
            //        bulkInsert.ColumnMappings.Add(rotateId);

            //        SqlBulkCopyColumnMapping standardSpeed = new SqlBulkCopyColumnMapping("ProTime", "ProTime");
            //        bulkInsert.ColumnMappings.Add(standardSpeed);

            //        SqlBulkCopyColumnMapping realitySpeed = new SqlBulkCopyColumnMapping("Clamptime", "Clamptime");
            //        bulkInsert.ColumnMappings.Add(realitySpeed);

            //        SqlBulkCopyColumnMapping process = new SqlBulkCopyColumnMapping("Note1", "Note1");
            //        bulkInsert.ColumnMappings.Add(process);

            //        SqlBulkCopyColumnMapping toKind = new SqlBulkCopyColumnMapping("Note2", "Note2");
            //        bulkInsert.ColumnMappings.Add(toKind);

            //        SqlBulkCopyColumnMapping note1 = new SqlBulkCopyColumnMapping("From", "From");
            //        bulkInsert.ColumnMappings.Add(note1);

            //        SqlBulkCopyColumnMapping note2 = new SqlBulkCopyColumnMapping("To", "To");
            //        bulkInsert.ColumnMappings.Add(note2);

            //        SqlBulkCopyColumnMapping note3 = new SqlBulkCopyColumnMapping("SProTime", "SProTime");
            //        bulkInsert.ColumnMappings.Add(note3);

            //        SqlBulkCopyColumnMapping note4 = new SqlBulkCopyColumnMapping("SClampTime", "SClampTime");
            //        bulkInsert.ColumnMappings.Add(note4);

            //        SqlBulkCopyColumnMapping note5 = new SqlBulkCopyColumnMapping("TOKQty", "TOKQty");
            //        bulkInsert.ColumnMappings.Add(note5);

            //        SqlBulkCopyColumnMapping note6 = new SqlBulkCopyColumnMapping("TNGQty", "TNGQty");
            //        bulkInsert.ColumnMappings.Add(note6);

            //        Cursor.Current = Cursors.WaitCursor;
            //        bulkInsert.WriteToServer(dr);
            //        Cursor.Current = Cursors.Default;

            //        DialogResult message = MessageBox.Show("Import successfully. Do you want to refresh data?",
            //                                               "Message", MessageBoxButtons.OKCancel);
            //        if (message == DialogResult.OK)
            //        {
            //            Reload(sender, e);
            //        }

            //    }
            //    catch (Exception exception)
            //    {
            //        MessageBox.Show(exception.Message);
            //    }
            //    finally
            //    {
            //        if (connection != null && connection.State == ConnectionState.Open)
            //        {
            //            connection.Close();
            //        }
            //    }
            //}

            #endregion

            ImportData();
        }

        private void ImportData()
        {
            INISettings mySetting = new INISettings();
            SqlConnection myConnection = new SqlConnection(mySetting.SQLConnectionString);

            FileDialog importFile = new OpenFileDialog();
            importFile.Filter = "Excel 2007-2013|*.xlsx|Excel 97-2003 (*.xls)|*.xls";
            if (importFile.ShowDialog() == DialogResult.OK)
            {
                OleDbConnection excelConnection = null;
                try
                {
                    Control parent = progressPanel1.Parent;
                    progressPanel1.Location =
                        new Point(parent.Bounds.X + parent.Bounds.Width / 2 - progressPanel1.Width / 2,
                                  parent.Bounds.Y + parent.Bounds.Height / 2 - progressPanel1.Height / 2);
                    progressPanel1.Visible = true;
                    string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                                                   importFile.FileName +
                                                   ";Extended Properties=Excel 12.0;Persist Security Info=False";
                    excelConnection = new OleDbConnection(excelConnectionString);
                    OleDbCommand cmd =
                        new OleDbCommand("Select [date],[Shift],[MachineID],[StaffID] ,[MONo] " +
                                         ",[OptionID] ,[WorkID] ,[time] ,[OKQty],[NGQty],[ProTime]" +
                                         ",[Clamptime],[Note1],[Note2],[From],[To],[SProTime],[SClampTime],[TOKQty],[TNGQty] from [Sheet1$]",
                                         excelConnection);
                    excelConnection.Open();
                    OleDbDataReader dReader;
                    dReader = cmd.ExecuteReader();
                    int i = 0;
                    //int countDup = 0;
                    //int line = 1;
                    //string lineDup = string.Empty;

                    myConnection.Open();
                    while (dReader.Read())
                    {
                        //bool exist = false;
                        //line++;
                        //try
                        //{
                        //    SqlCommand cmd_sql = myConnection.CreateCommand();
                        //    cmd_sql.CommandText = "SELECT * FROM [242_LaborWTS] " +
                        //                          "WHERE [date]='" + dReader["date"].ToString() + "' " +
                        //                          " and [Shift]='" + dReader["Shift"].ToString() + "'" +
                        //                          " and [MachineID]=N'" + dReader["MachineID"].ToString() + "'" +
                        //                          " and [StaffID]='" + dReader["StaffID"].ToString() + "'" +
                        //                          " and [MONo]='" + dReader["MONo"].ToString() + "'" +
                        //                          " and [OptionID]='" + dReader["OptionID"].ToString() + "'" +
                        //                          " and [WorkID]='" + dReader["WorkID"].ToString() + "'" +
                        //                          " and [time]=" + dReader["time"].ToString() +
                        //                          " and [OKQty]=" + dReader["OKQty"].ToString() + 
                        //                          " and [NGQty]=" + dReader["NGQty"].ToString() + 
                        //                          " and [ProTime]=" + dReader["ProTime"].ToString() + 
                        //                          " and [Clamptime]=" + dReader["Clamptime"].ToString() +
                        //                          " and [Note1]=N'" + dReader["Note1"].ToString() + "'" +
                        //                          " and [Note2]=N'" + dReader["Note2"].ToString() + "'" +
                        //                          " and [From]='" + dReader["From"].ToString() + "'" +
                        //                          " and [To]='" + dReader["To"].ToString() + "'" +
                        //                          " and [SProTime]=" + dReader["SProTime"].ToString() + 
                        //                          " and [SClampTime]=" + dReader["SClampTime"].ToString() +
                        //                          " and [TOKQty]=" + dReader["TOKQty"].ToString() + 
                        //                          " and [TNGQty]=" + dReader["TNGQty"].ToString();
                        //    SqlDataReader dataReader = cmd_sql.ExecuteReader();
                        //    if (dataReader.Read())
                        //    {

                        //        exist = true;
                        //        countDup++;

                        //        lineDup += line.ToString() + ", ";
                        //    }
                        //    dataReader.Close();
                        //    cmd_sql.Dispose();
                        //}
                        //catch
                        //{
                        //    exist = true;
                        //}

                        //if (!exist)
                        //{

                        string date = (dReader["date"].ToString().Equals(string.Empty)) ? "null" : "'" + dReader["date"].ToString() + "'";
                        string shift = (dReader["Shift"].ToString().Equals(string.Empty)) ? "null" : "N'" + dReader["Shift"].ToString() + "'";
                        string staffID = (dReader["StaffID"].ToString().Equals(string.Empty)) ? "null" : "N'" + dReader["StaffID"].ToString() + "'";
                        string machineID = (dReader["MachineID"].ToString().Equals(string.Empty)) ? "null" : "N'" + dReader["MachineID"].ToString() + "'";
                        string mONo = (dReader["MONo"].ToString().Equals(string.Empty)) ? "null" : "N'" + dReader["MONo"].ToString() + "'";
                        string optionID = (dReader["OptionID"].ToString().Equals(string.Empty)) ? "null" : "N'" + dReader["OptionID"].ToString() + "'";
                        string workID = (dReader["WorkID"].ToString().Equals(string.Empty)) ? "null" : "N'" + dReader["WorkID"].ToString() + "'";
                        string time = (dReader["time"].ToString().Equals(string.Empty)) ? "null" : dReader["time"].ToString();
                        string oKQty = (dReader["OKQty"].ToString().Equals(string.Empty)) ? "null" : dReader["OKQty"].ToString();
                        string nGQty = (dReader["NGQty"].ToString().Equals(string.Empty)) ? "null" : dReader["NGQty"].ToString();
                        string proTime = (dReader["ProTime"].ToString().Equals(string.Empty)) ? "null" : dReader["ProTime"].ToString();
                        string clamptime = (dReader["Clamptime"].ToString().Equals(string.Empty)) ? "null" : dReader["Clamptime"].ToString();
                        string note1 = (dReader["Note1"].ToString().Equals(string.Empty)) ? "null" : "N'" + dReader["Note1"].ToString() + "'";
                        string note2 = (dReader["Note2"].ToString().Equals(string.Empty)) ? "null" : "N'" + dReader["Note2"].ToString() + "'";
                        string from = (dReader["From"].ToString().Equals(string.Empty)) ? "null" : "N'" + dReader["From"].ToString() + "'";
                        string to = (dReader["To"].ToString().Equals(string.Empty)) ? "null" : "N'" + dReader["To"].ToString() + "'";
                        string sProTime = (dReader["SProTime"].ToString().Equals(string.Empty)) ? "null" : dReader["SProTime"].ToString();
                        string sClampTime = (dReader["SClampTime"].ToString().Equals(string.Empty)) ? "null" : dReader["SClampTime"].ToString();
                        string tOKQty = (dReader["TOKQty"].ToString().Equals(string.Empty)) ? "null" : dReader["TOKQty"].ToString();
                        string tNGQty = (dReader["TNGQty"].ToString().Equals(string.Empty)) ? "null" : dReader["TNGQty"].ToString();


                        string sql = "INSERT INTO [242_LaborWTS] ("
                                     + "[date],[Shift],[StaffID],[MachineID] ,[MONo] "
                                     + ",[OptionID] ,[WorkID] ,[time] ,[OKQty],[NGQty],[ProTime]"
                                     + ",[Clamptime],[Note1],[Note2],[From],[To],[SProTime],[SClampTime],[TOKQty],[TNGQty] )"
                                     + " VALUES (" + date + ", " + shift + ", " + staffID + ","
                                     + machineID + "," + mONo + ", " + optionID + ", " + workID + ", " + time + ", "
                                     + oKQty + " ," + nGQty + ", " + proTime + ", " + clamptime + ", " + note1 + ", " + note2 + ", "
                                     + from + ", " + to + ", " + sProTime + ", " + sClampTime + ", " + tOKQty + ", " + tNGQty + ")";
                        SqlCommand cmd_sql = new SqlCommand(sql, myConnection);

                        if (cmd_sql.ExecuteNonQuery() > 0)
                        {

                            //write Log
                            log.Table = LogConstant.Table.Product.LaborWTS;
                            log.Task = LogConstant.Task.Import;
                            log.Description = "Date: " + date + "; MONo: " + mONo + "; MachineID: " + machineID + "; OptionID: "
                                        + optionID + "; Shift: " + shift + "; WorkID: " + workID + "; Time: " + time
                                        + "; ProTime: " + proTime + "; ClampTime: " + clamptime;
                            log.WriteLog();

                        }
                        cmd_sql.Dispose();
                        i++;

                        //}
                    }
                    myConnection.Close();
                    progressPanel1.Visible = false;
                    excelConnection.Close();
                    MessageBox.Show(i + " records have been imported.");
                    //MessageBox.Show(i + " records have been imported. \n" + countDup.ToString() + " record(s) duplicated (Lines: " + lineDup.Substring(0, lineDup.Length - 1) + ")");
                    //LoadData();
                }
                catch (Exception ex)
                {
                    if (excelConnection.State == ConnectionState.Open)
                    {
                        excelConnection.Close();
                    }
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    MessageBox.Show(ex.Message);
                }

            }
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            dt = clsWTSDAO.ChiaKhoangLabor(dateForm.DateTime.Date, dateTo.DateTime.Date);
            gridControl1.DataSource = dt;
            gridView1.BestFitColumns();
            Cursor.Current = Cursors.Default;
        }


        private void dropbtExport_Click(object sender, EventArgs e)
        {

        }

        //Editable
        private void gridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            GridView view = sender as GridView;


            //Kiểm tra đây là dòng dữ liệu mới hay cũ, nếu là mới thì mình insert
            if (view.IsNewItemRow(e.RowHandle))
            {
                //e.RowHandle trả về giá trị int là thứ tự của dòng hiện tại

            }
            //Cũ thì update
            else
            {
                string date = (view.GetRowCellValue(e.RowHandle, view.Columns["date"]).ToString() == "") ? "null" : "'" + view.GetRowCellValue(e.RowHandle, view.Columns["date"]).ToString() + "'";
                string shift = (view.GetRowCellValue(e.RowHandle, view.Columns["Shift"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["Shift"]).ToString() + "'";
                string staffID = (view.GetRowCellValue(e.RowHandle, view.Columns["StaffID"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["StaffID"]).ToString() + "'";
                string machineID = (view.GetRowCellValue(e.RowHandle, view.Columns["MachineID"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["MachineID"]).ToString() + "'";
                string mONo = (view.GetRowCellValue(e.RowHandle, view.Columns["MONo"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["MONo"]).ToString() + "'";
                string optionID = (view.GetRowCellValue(e.RowHandle, view.Columns["OptionID"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["OptionID"]).ToString() + "'";
                string workID = (view.GetRowCellValue(e.RowHandle, view.Columns["WorkID"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["WorkID"]).ToString() + "'";
                string time = (view.GetRowCellValue(e.RowHandle, view.Columns["time"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["time"]).ToString();
                string oKQty = (view.GetRowCellValue(e.RowHandle, view.Columns["OKQty"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["OKQty"]).ToString();
                string nGQty = (view.GetRowCellValue(e.RowHandle, view.Columns["NGQty"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["NGQty"]).ToString();
                string proTime = (view.GetRowCellValue(e.RowHandle, view.Columns["ProTime"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["ProTime"]).ToString();
                string clamptime = (view.GetRowCellValue(e.RowHandle, view.Columns["Clamptime"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["Clamptime"]).ToString();
                string note1 = (view.GetRowCellValue(e.RowHandle, view.Columns["Note1"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["Note1"]).ToString() + "'";
                string note2 = (view.GetRowCellValue(e.RowHandle, view.Columns["Note2"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["Note2"]).ToString() + "'";
                string from = (view.GetRowCellValue(e.RowHandle, view.Columns["From"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["From"]).ToString() + "'";
                string to = (view.GetRowCellValue(e.RowHandle, view.Columns["To"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["To"]).ToString() + "'";
                string sProTime = (view.GetRowCellValue(e.RowHandle, view.Columns["SProTime"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["SProTime"]).ToString();
                string sClampTime = (view.GetRowCellValue(e.RowHandle, view.Columns["SClampTime"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["SClampTime"]).ToString();
                string tOKQty = (view.GetRowCellValue(e.RowHandle, view.Columns["TOKQty"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["TOKQty"]).ToString();
                string tNGQty = (view.GetRowCellValue(e.RowHandle, view.Columns["TNGQty"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["TNGQty"]).ToString();
                string id = (view.GetRowCellValue(e.RowHandle, view.Columns["ID"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["ID"]).ToString();

                string update = "update [242_LaborWTS] set [date]=" + date
                    + ", Shift = " + shift
                    + ", StaffID = " + staffID
                    + ", [MachineID] = " + machineID
                    + ", [MONo] = " + mONo
                    + ", [OptionID] = " + optionID
                    + ", [WorkID] = " + workID
                    + ", [time] = " + time
                    + ", [OKQty] = " + oKQty
                    + ", [NGQty] = " + nGQty
                    + ", [ProTime] = " + proTime
                    + ", [Clamptime] = " + clamptime
                    + ", [Note1] = " + note1
                    + ", [Note2] = " + note2
                    + ", [From] = " + from
                    + ", [To] = " + to
                    + ", [SProTime] = " + sProTime
                    + ", [SClampTime] = " + sClampTime
                    + ", [TOKQty] = " + tOKQty
                    + ", [TNGQty] = " + tNGQty
                    + " where [ID] = " + id;
                if (BaseDAO.ExecuteNonQuery(update) > 0)
                {

                    //write Log
                    log.Table = LogConstant.Table.Product.LaborWTS;
                    log.Task = LogConstant.Task.Update;
                    log.Description = "Date: " + date + "; MONo: " + mONo + "; MachineID: " + machineID + "; OptionID: "
                                + optionID + "; Shift: " + shift + "; WorkID: " + workID + "; Time: " + time
                                + "; ProTime: " + proTime + "; ClampTime: " + clamptime;
                    log.WriteLog();

                }

            }
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt)
            {
                if (e.KeyCode == Keys.E)
                {
                    if (gridView1.OptionsBehavior.Editable)
                    {
                        //gridView1.fo;
                        gridView1.OptionsBehavior.Editable = false;
                    }
                    else
                    {
                        gridView1.OptionsBehavior.Editable = true;
                    }
                }
            }
        }

        private void btnWTSFind_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            frm_LaborWTSFind frmFind = new frm_LaborWTSFind(this);
            frmFind.Show();
            Cursor.Current = Cursors.Default;
        }

        public void BindDatatoGridFind(DataTable dtFind)
        {
            gridControl1.DataSource = dtFind;
            gridView1.BestFitColumns();
        }

        public void SelectRow()
        {
            gridView1.SelectRow(0);
            gridView1.FocusedRowHandle = (0);
            this.ProcessTabKey(true);
        }
    }
}
